package com.gwm.item.controller;

import com.gwm.common.vo.PageResult;

import com.gwm.item.dto.SkuDTO;
import com.gwm.item.dto.SpuDTO;
import com.gwm.item.dto.SpuDetailDTO;
import com.gwm.item.service.impl.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 查询spu列表
     * @param page
     * @param rows
     * @param key
     * @param saleable
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageResult> findSpuByPage(
            @RequestParam(name = "page",defaultValue = "1") Integer page,
            @RequestParam(name = "rows",defaultValue = "5") Integer rows,
            @RequestParam(name = "key",required = false) String key,
            @RequestParam(name = "saleable",required = false) Boolean saleable){
        PageResult<SpuDTO> pageResult = goodsService.findSpuPage(page,rows,key,saleable);
        return ResponseEntity.ok(pageResult);
    }

    @PostMapping(value = "/goods",name = "保存商品")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO){
        goodsService.saveGoods(spuDTO);
        return ResponseEntity.ok().build();
    }

    @PutMapping(value = "/goods",name = "修改商品")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO){
        goodsService.updateGoods(spuDTO);
        return ResponseEntity.ok().build();
    }
    @PutMapping(value = "/spu/saleable",name = "修改商品上下架")
    public ResponseEntity<Void> updateGoodsSaleable(
            @RequestParam("id") Long spuId,
            @RequestParam("saleable") Boolean saleable
    ){
        goodsService.updateGoodsSaleable(spuId,saleable);
        return ResponseEntity.ok().build();
    }

    @GetMapping(value = "/spu/detail",name = "根据spuId查询SPUDetail")
    public ResponseEntity<SpuDetailDTO> findSpuDetailBySpuId(
            @RequestParam("id") Long spuId
    ){
        SpuDetailDTO spuDetailDTO = goodsService.findSpuDetailBySpuId(spuId);
        return ResponseEntity.ok(spuDetailDTO);
    }


    @GetMapping(value = "/sku/of/spu",name = "根据spuId查询sku集合")
    public ResponseEntity<List<SkuDTO>> findSkuListBySpuId(
            @RequestParam("id") Long spuId
    ){
        List<SkuDTO>  skuDTOList = goodsService.findSkuListBySpuId(spuId);
        return ResponseEntity.ok(skuDTOList);
    }


    @GetMapping(value = "/spu/{id}",name = "根据spuId查询SPU")
    public ResponseEntity<SpuDTO> findSpuBySpuId(@PathVariable("id") Long spuId){
        SpuDTO spuDTO = goodsService.findSpuBySpuId(spuId);
        return ResponseEntity.ok(spuDTO);
    }

//    /**
//     * 根据id批量查询sku
//     * @param ids skuId的集合
//     * @return sku的集合
//     */
//    @GetMapping("sku/list")
//    public ResponseEntity<List<SkuDTO>> querySkuByIds(@RequestParam("ids") List<Long> ids) {
//        return ResponseEntity.ok(this.goodsService.querySkuListByIds(ids));
//    }


    @GetMapping(value = "/sku/list",name = "根据skuId集合查询sku集合")
    public ResponseEntity<List<SkuDTO>> findSkuListBySkuIds(
            @RequestParam("ids") List<Long> ids
    ){
        List<SkuDTO>  skuDTOList = goodsService.findSkuListBySkuIds(ids);
        return ResponseEntity.ok(skuDTOList);
    }

    @PutMapping("/stock/minus")
    public ResponseEntity<Void> stockMinus(@RequestBody Map<Long, Integer> skuIdAndNumMap){
        goodsService.stockMinus(skuIdAndNumMap);
        return ResponseEntity.ok().build();
    }

    @PutMapping("/stock/plus")
    public ResponseEntity<Void> stockPlus(@RequestBody Map<Long, Integer> skuIdAndNumMap){
        goodsService.stockPlus(skuIdAndNumMap);
        return ResponseEntity.ok().build();
    }


    /**
     * 根据brandid查询商品
     * @param brandId
     * @param cid3
     * @return
     */
    @GetMapping("/spu/for/brand")
    public ResponseEntity<List<SpuDTO>> findSpuByBrandId(@RequestParam(name = "id") Long brandId,
                                                         @RequestParam(name = "cid") Long cid3){
        return ResponseEntity.ok(goodsService.findSpuByBrandId(brandId,cid3));
    }












}